/*
    Project:        Home Power Control
    File:           HPCAnemometer.h
    Author:         christian@karunamusic.at
    Date:           11.2014
    Description:    Sensor to measure the speed of wind.
                    The sensor gives one tick per rotation.
                    One tick per second means a wind speed of 2,4km/h.
 */

#ifndef HPCANEMOMETER_H
#define HPCANEMOMETER_H

#include <boost/thread/thread.hpp>
#include <HPCThreadedSensor.h>


/** \brief Sensor to measure the speed of wind.
 *  Gives the speed of wind in km/h (or beaufort).
 *  Count the ticks of the sensor (via interrupt) in the given intervall-time.
 */
class HPCAnemometer : public HPCThreadedSensor
{
public:
    /** \brief Constructor
     *
     * \param id database id of the sensor.
     * \param pin GPIO pin number.
     * \param interval time (in seconds) to pause between querying the sensor.
     *
     */
    HPCAnemometer( unsigned long id, unsigned int pin, unsigned int interval );
    /** \brief Destructor.
     */
    virtual ~HPCAnemometer();

    /** \brief Get the type of the sensor.
     *
     * \return SensorType Type of the sensor (from enum SensorType).
     *
     */
    virtual inline SensorType Type() const
      { return SensorType::HPCAnemometer; }

    /** \brief Get the speed of wind in beaufort.
     *
     * \return Speed of wind in beaufort.
     *
     */
    unsigned int Beaufort() const;

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

    /** \brief Check if sensor has changed (called after interval pause).
     *
     * \return True if changed.
     */
    virtual bool hasChanged();

    /** \brief Check if sensor is beyond limits.
     *
     * \return int 0 if no alarm, -1 if below limit, +1 if above limit.
     */
    virtual int isAlarm();

private:
    static unsigned int m_Counter; /**< count the ticks of the sensor in the given time. */

    /** \brief Interrupt service routine to count ticks from the sensor.
     */
    static void isr();
};

#endif // HPCANEMOMETER_H
